package uk.ac.strath.cis.religionsearch.view;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import uk.ac.strath.cis.religionsearch.controller.Controller;

public class IndexWindow implements ActionListener{
	private JFrame indexFrame;
	private JPanel indexPanel;
	private JTextField indexLocField, 
					   indexDestField;
	private JButton browseBtn,
					browseBtnDest,
					startIndexingBtn;
	private Controller c;
	private JFileChooser indexChooser,
						 indexDestChooser;
	private JLabel instructions,
				   destinationInstructions;
	
	public IndexWindow(Controller _c){
		c=_c;
		indexChooser = new JFileChooser();
		indexChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}
	
	public JFrame getIndexWindow(){
		if(indexFrame==null){
			indexFrame=new JFrame("Index Manager");
			indexFrame.setMinimumSize(new Dimension(300, 200));
			indexFrame.setPreferredSize(new Dimension(500, 200));
			indexFrame.setResizable(false);
			indexFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			indexFrame.setLocation(new Point(20, 20));
			indexFrame.add(getIndexPanel());
			
		}
		return indexFrame;
	}
	
	private JPanel getIndexPanel(){
		if(indexPanel==null){
			indexPanel = new JPanel();
			
			instructions = new JLabel("Please select a folder containing the xml you wish to index");
			destinationInstructions = new JLabel("Please select a folder for the index output");
			
			indexLocField = new JTextField(40);
			indexLocField.setText(c.getIndexLoc());
			
			indexDestField = new JTextField(40);
			indexDestField.setText(c.getIndexLoc() + File.separator + "index");
			
			browseBtn = new JButton();
			browseBtn.setText("Browse");
			browseBtn.setActionCommand("browse");
			browseBtn.addActionListener(this);
			
			browseBtnDest = new JButton();
			browseBtnDest.setText("Browse");
			browseBtnDest.setActionCommand("browseDest");
			browseBtnDest.addActionListener(this);
			
			startIndexingBtn = new JButton();
			startIndexingBtn.setText("Start Indexing");
			startIndexingBtn.setActionCommand("startindexing");
			startIndexingBtn.addActionListener(this);
			
			indexPanel.add(instructions);
			indexPanel.add(indexLocField);
			indexPanel.add(browseBtn);
			indexPanel.add(destinationInstructions);
			indexPanel.add(indexDestField);
			indexPanel.add(browseBtnDest);
			indexPanel.add(startIndexingBtn);
		}
		return indexPanel;
	}
	
	private String setIndexInputLocation(){
		indexChooser.setCurrentDirectory(new File(indexLocField.getText()));
		int returnVal = indexChooser.showOpenDialog(this.getIndexWindow());
		if(returnVal == JFileChooser.APPROVE_OPTION)
		{
			String loc = indexChooser.getSelectedFile().getAbsolutePath().toString();
		this.indexLocField.setText(loc);
			return loc;
		}
		return null;
	}
	
	private String setIndexOutputLocation(){
		indexChooser.setCurrentDirectory(new File(indexDestField.getText()));
		int returnVal = indexChooser.showOpenDialog(this.getIndexWindow());
		if(returnVal == JFileChooser.APPROVE_OPTION)
		{
			String loc = indexChooser.getSelectedFile().getAbsolutePath().toString();
		this.indexDestField.setText(loc);
			return loc;
		}
		return null;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("browse")){
			c.setIndexLoc(setIndexInputLocation());
		} else if(e.getActionCommand().equals("browseDest")){
			c.setIndexDest(setIndexOutputLocation());
		} else if(e.getActionCommand().equals("startindexing")){
			c.startIndexing();
		}
		
	}
	
}
